/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.netmaster.coitaonline.util.pdf;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.netmaster.coitaonline.pojo.Usuario;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sergio
 */
public class ExportTicketPdf {

    private static final Font lNormal = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);
    private static final Font lNegra = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
    private static final String HEADER_NETMASTER = "NETMASTER INTERNET INALAMBRICO TEL:(968)68-8-09-60 y 9616199756 ";
    private static final String HEADER_ADDRESS = "3a. SUR ENTRE 1a. y 2a. ORIENTE #12A OCOZOCOAUTLA DE ESPINOSA CHIAPAS ";

    public ByteArrayOutputStream inicio(Usuario[] users,Date fecha) {
        Document doc = new Document(PageSize.A7, 1, 1, 1, 1);
        PageSize a = new PageSize();
        ByteArrayOutputStream file = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(doc, file);
            doc.open();
            addMetaData(doc);
            for (Usuario user : users) {
                addTitlePage(doc, user,fecha);
            }
            doc.close();
        } catch (DocumentException ex) {
            Logger.getLogger(ExportTicketPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
        return file;
    }

    private void addMetaData(Document document) {
        document.addTitle("My first PDF");
        document.addSubject("Using iText");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private void addTitlePage(Document document,Usuario user,Date fecha) throws DocumentException {
        Paragraph preface = new Paragraph();
        addHeader(preface);
        // Will create: Report generated by: _name, _date
        Paragraph x = new Paragraph();
        x.setAlignment(Element.ALIGN_CENTER);
        x.setFont(lNegra);
        SimpleDateFormat df = new SimpleDateFormat("MMMM");
        x.add(df.format(fecha));
        preface.add(x);

        Paragraph centro = new Paragraph();
        centro.setAlignment(Element.ALIGN_CENTER);
        centro.setFont(lNormal);
        Calendar cale = Calendar.getInstance();
        Calendar cale2 = Calendar.getInstance();
        SimpleDateFormat fcobro = new SimpleDateFormat("dd/MM/yyyy");
        cale.setTime(fecha);
        cale2.setTime(fecha);
        cale.set(Calendar.DATE, user.getFechaCobro());
        cale2.set(Calendar.DATE, user.getFechaCobro());
        cale.add(Calendar.MONTH, 1);
        centro.add(fcobro.format(cale2.getTime())+" AL "+fcobro.format(cale.getTime())+" \n");
        
        centro.add(" Fecha de cobro = "+user.getFechaCobro()+"\n");
        centro.add(user.getServidor().getNombre());
        preface.add(centro);

        Paragraph usuario = new Paragraph();
        usuario.setAlignment(Element.ALIGN_CENTER);
        usuario.setFont(lNegra);
        usuario.add(user.getNombre());
        preface.add(usuario);

        Paragraph direccion = new Paragraph();
        direccion.setAlignment(Element.ALIGN_CENTER);
        direccion.setFont(lNormal);
        direccion.add(user.getDireccion());
        direccion.add(user.getColonia().getNombre());
        preface.add(direccion);

        Paragraph costo = new Paragraph();
        costo.setAlignment(Element.ALIGN_CENTER);
        costo.setFont(lNegra);
        costo.add(""+user.getPrecio());
        preface.add(costo);

        Paragraph fin = new Paragraph();
        fin.setAlignment(Element.ALIGN_CENTER);
        fin.setFont(lNormal);
        fin.add("doscientoss pesos 00/100 m.n. ");
        fin.add(" Este comprobante no es valido para efectos fiscales");
        preface.add(fin);

        document.add(preface);
        // Start a new page
        document.newPage();

    }

    private void addHeader(Paragraph preface) {
        preface.setAlignment(Element.ALIGN_CENTER);
        // Lets write a big header
        Paragraph header = new Paragraph();
        header.setAlignment(Element.ALIGN_CENTER);
        header.setFont(lNormal);
        header.add(HEADER_NETMASTER);
        preface.add(header);

        Paragraph header2 = new Paragraph();
        header2.setAlignment(Element.ALIGN_CENTER);
        header2.setFont(lNormal);
        header2.add(HEADER_ADDRESS);
        preface.add(header2);

        addEmptyLine(preface, 1);
    }

    private void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }
}
